﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;

public partial class ShoppingCart : System.Web.UI.Page
{
    CartData cart;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //get the cart from the Session and rebind the cart to the GridView
            this.cart = CartData.getCartFromSession(Page);
            if (!Master.CheckLogin())
            {
                this.cart.reset();
                CartData.saveCartToSession(Page, cart);
            }
            bindCartGridView();
        }//end if

    }//end Page_Load

    private void bindCartGridView()
    {
        if (this.cart == null)
        {
            this.cart = CartData.getCartFromSession(Page);
        }//end if

        //Bind the DataTable from the CartData object to the GridView
        cartGridView.DataSource = this.cart.getCartTable();
        cartGridView.DataBind();

        //set the totalLabel text to the value of the shopping cart total
        totalLabel.Text = String.Format("{0:C}",cart.getTotal());
    }//end bindCartGridView

    protected void cartGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        cartGridView.EditIndex = -1;
        bindCartGridView();

    }
    protected void cartGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string isbn;

        //get the cart from the Session
        this.cart = CartData.getCartFromSession(Page);

        //get the ISBN from the hidden DataKeys field of the GridView
        isbn = cartGridView.DataKeys[e.RowIndex].Value.ToString();

        //remove the item from the cart
        this.cart.removeItem(isbn);

        //save the cart to the session and rebind it to the GridView        
        CartData.saveCartToSession(Page, this.cart);
        bindCartGridView();

    }
    protected void cartGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //set the index of the row to edit
        cartGridView.EditIndex = e.NewEditIndex;

        //get the cart from the Session and rebind the cart to the GridView
        this.cart = CartData.getCartFromSession(Page);
        bindCartGridView();
    }
    protected void cartGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string isbn;
        short quantity;
        TextBox tb;

        //get the cart from the Session
        this.cart = CartData.getCartFromSession(Page);

        //get the value of the TextBox in the quantity column
        tb = (TextBox) cartGridView.Rows[e.RowIndex].Cells[2].Controls[1];
        Int16.TryParse(tb.Text,out quantity);

        //get the ISBN from the hidden DataKeys field of the GridView
        isbn = cartGridView.DataKeys[e.RowIndex].Value.ToString();

        //save the new quantity to the cart
        this.cart.setQuantity(isbn,quantity);

        //end the edit session
        cartGridView.EditIndex = -1;

        //save the cart to the session and rebind it to the GridView        
        CartData.saveCartToSession(Page,cart);
        bindCartGridView();

    }//end cartGridView_RowUpdating
    protected void checkoutButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("Checkout.aspx");
    }
    protected void continueButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}//end class ShoppingCart
  